package main

import (
	//"./utils"
	"fmt"
	"strings"
	"time"
)

var timeFormat = "2006-01-02 15:04:05"

//将日期字符串解析为系统time
func ParseStringToTime(str string) (time.Time, error) {
	loc, err := time.LoadLocation("Local")
	if err != nil {
		return time.Now(), err
	}
	t, err2 := time.ParseInLocation(timeFormat, str, loc)
	if err2 != nil {
		return time.Now(), err2
	}

	return t, nil
}

//将指定时间转化为指定格式字符串：yyyy-MM-dd HH:mm:ss
func FormatTimeToString(t time.Time) string {
	return t.Format(timeFormat)
}

func functionB() {
	now := time.Now()
	fmt.Println("now:", now)

	// Local返回采用本地和本地时区，但指向同一时间点的Time
	now = time.Now().Local()
	fmt.Println("now:", now)
	fmt.Println("now-unix:", now.Unix())
	// 计算下一个零点
	next := now.Add(time.Hour * 24)
	next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
	fmt.Println("next:", next)
	result := next.Sub(now)
	fmt.Println("result:", result)

	todayTime := time.Now()

	//取得第二天的相同时间的时间戮
	tomorrowTime := todayTime.Add(24 * time.Hour)
	tomorrowStr := FormatTimeToString(tomorrowTime)
	tomorrowStrs := strings.Split(tomorrowStr, " ")
	tomorrowStr = tomorrowStrs[0] + " 02:00:00"

	tomorrowTime, err := ParseStringToTime(tomorrowStr)
	if err == nil {
		sleepTime := tomorrowTime.Sub(todayTime).Seconds()
		//time.Sleep(sleepTime * time.Second)

		fmt.Println("sleepTime:", sleepTime)
	}

	timenow := time.Now()

	//重置当前时间为当日00:00
	timenow = time.Date(timenow.Year(), timenow.Month(), timenow.Day(), 0, 0, 0, 0, timenow.Location())
	fmt.Println("timenow:", timenow)

	fmt.Println("Day:", fmt.Sprintf("%v-%v-%02v", timenow.Year(), timenow.Month(), timenow.Day()))
	fmt.Println("Day2:", strings.Split(FormatTimeToString(timenow), " ")[0])
}
func main() {
	//utils.FuncAfterFunc()
	functionB()
}
